﻿using System;
using System.Collections.Generic;
using System.Web;
using Antu.Core.DataAccess;

namespace WebSys.UI.OfficeFlow
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
    [System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile file = context.Request.Files["Filedata"];
            string strUrl = context.Request.FilePath;
            string strtt = HttpContext.Current.Request["temp"];
            string[] ttarray=strtt.Split('*');

            string strIId = ttarray[0];
            string strWIId = ttarray[1];
            string strUserId = ttarray[2];
            string strPath = ttarray[3];
            
            System.IO.Stream inputstr = file.InputStream;
            byte[] FileContent = new byte[inputstr.Length];
            inputstr.Seek(0, System.IO.SeekOrigin.Begin);
            inputstr.Read(FileContent, 0, Convert.ToInt32(inputstr.Length));
            if (file != null)
            {
                UpdateFiles(file, strUrl, FileContent, strIId, strWIId, strUserId, strPath);
                context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
            inputstr.Dispose();
        }
        public void UpdateFiles(HttpPostedFile postfile, string strUrl, byte[] bFileContent, string strIId, string strWIId, string strUserId, string strPath)
        {
            Antu.WorkFlowFactory.FlowFactoryProvider _FlowFactoryProvider = (Antu.WorkFlowFactory.FlowFactoryProvider)Antu.Core.Services.ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
            try
            {
                string fileName = System.IO.Path.GetFileName(postfile.FileName);
                string fileext = fileName.Substring(fileName.LastIndexOf("."));
                string fname = fileName.Replace(fileext, "");
                if (bFileContent.Length > 0)
                {
                    string strAId = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                    _FlowFactoryProvider.InsertFiles(strAId, fname, fileext,strPath, strIId, strWIId, strUserId);
                    _FlowFactoryProvider.UpdateFileData(strAId, bFileContent);
                }
            }
            catch { }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
